<?php
/**
 * @file : articlevoteproperty.php
 * @date : 2015-02-04 10:08:16
 */
namespace haibao\cms\data;

use haibao\frame\data\query\Condition;
use \haibao\cms\model\filter\ArticleVoteProperty as ArticleVotePropertyFilter;

class ArticleVoteProperty extends \haibao\cms\data\BaseMysql{

    public function __construct(){
        parent::__construct('\haibao\cms\model\data\ArticleVoteProperty');
    }

	/**
	 * 修改文章投票属性
	 * @param : object \haibao\cms\model\data\ArticleVote
	 */
	public function editArticleVoteProperty($articleVoteModel){

		$votePropertyIdArr = array();
		// 更新投票属性
		foreach($articleVoteModel->ArticleVoteProperty as $property){
		
			$votePropertyFilter = new ArticleVotePropertyFilter();
			$votePropertyFilter->where(ArticleVotePropertyFilter::CONDITION_FIELD_NAME_ID,Condition::CONDITION_EQUAL,$property->Id);
			$this->update(
				array(
					ArticleVotePropertyFilter::CONDITION_FIELD_NAME_TITLE => $property->Title,
					ArticleVotePropertyFilter::CONDITION_FIELD_NAME_ORDER_NUM => $property->OrderNum,
					ArticleVotePropertyFilter::CONDITION_FIELD_NAME_DES => $property->Des,
				),
				$votePropertyFilter
			);
			$votePropertyIdArr[] = $property->Id;
		}
		
		// 删除已删除的投票属性
		if($votePropertyIdArr){
			$sql = 'delete from `ArticleVoteProperty` where ArticleVoteId=%s and Id not in ('.implode(',',$votePropertyIdArr).')';
			$this->query($sql,array($articleVoteModel->Id));
		}else{
			$sql = 'delete from `ArticleVoteProperty` where ArticleVoteId=%s';
			$this->query($sql,array($articleVoteModel->Id));
		}
		
		// 添加新的投票属性
		if($articleVoteModel->ArticleVotePropertyNew){
			foreach($articleVoteModel->ArticleVotePropertyNew as $property){

				$this->add($property);
			}
			//$this->addAll($articleVoteModel->ArticleVotePropertyNew);
		}
	}
}
